package com.kanas.webak.organization.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kanas.app.viewobject.PlainTree;
import com.kanas.framework.business.BusinessHandler;
import com.kanas.framework.business.ResponseMessage;
import com.kanas.framework.support.ControllerSupport;
import com.kanas.webak.organization.domain.Organization;
import com.kanas.webak.organization.service.OrganizationService;

@Controller
@RequestMapping("app/organization/org")
public class OrganizationController extends ControllerSupport{
	private OrganizationService organizationService;

	public OrganizationService getOrganizationService() {
		return organizationService;
	}
	
	@Resource(name="kanas.admin.service.organization")
	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}
	
	@RequestMapping(value="/{pid}/new.html",method=RequestMethod.GET)
	public ModelAndView addValueNew(@PathVariable("pid")String pId){
		ModelAndView mv=new ModelAndView();
		mv.addObject("pId", pId);
		mv.setViewName("modules/organization/organization_new");
		return mv;
	}
	
	@RequestMapping("/new.do")
	public @ResponseBody ResponseMessage addNew(final Organization org,@RequestParam(value="pid",required=false) final String pid) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				String pId=null;
				if(!pid.equals("root")){
					pId = pid;
				}
				return organizationService.addNew(org,pId);
			}
		}, logger);
		return response;
	}
	
	@RequestMapping(value="/org.html")
	public ModelAndView orgView(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/organization/organization");
		return mv;
	}
	
	@RequestMapping(value="/edit/{orgId}.html")
	public ModelAndView ddValueView(@PathVariable("orgId")String oid){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/organization/organization_edit");
		Organization org=organizationService.findOne(oid);
		mv.addObject("org", org);
		return mv;
	}
	
	@RequestMapping("/{orgid}/edit.do")
	public @ResponseBody ResponseMessage update(final Organization org,@PathVariable("orgid")final String oid) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				org.setOldId(oid);
				return organizationService.update(org);
			}
		}, logger);
		return response;
	}
	
	@RequestMapping("/{orgid}")
	public @ResponseBody ResponseMessage findOne(@PathVariable("orgid")final String oid) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				Organization o=organizationService.findOne(oid);
				o.setChildrens(null);
				o.setParent(null);
				o.setUserRoles(null);
				return o;
			}
		}, logger);
		return response;
	}
	
	@RequestMapping(value="/{orgid}",method=RequestMethod.DELETE)
	public @ResponseBody String deleteOrg(@PathVariable("orgid") final String oid){
		ResponseMessage rm = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return organizationService.remove(oid);
			}
		}, logger);
		return rm.toString();
	}
	
	@RequestMapping(value="/list.json",method=RequestMethod.GET)
	public @ResponseBody List<PlainTree> listDictValue(){
		List<PlainTree> response=new ArrayList<PlainTree>();
		PlainTree treeNode=new PlainTree("root","all","");
		response.add(treeNode);
		List<Organization> orgs=organizationService.findAll();
		for(Organization o:orgs){
			treeNode=new PlainTree(o.getId(),o.getLabel(),o.getParent()==null?"root":o.getParent().getId());
			response.add(treeNode);
		}
		return response;
	}
}
